package com.bcurtu.amigo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bcurtu.amigo.pojo.Amigo;
import com.bcurtu.amigo.services.AmigoInvisibleBO;
import com.bcurtu.amigo.services.EncodingException;
import com.bcurtu.amigo.services.GroupIdEncrypter;

public class AutoForwarderController extends AbstractController {

	private AmigoInvisibleBO bo;
	Logger log=Logger.getLogger(this.getClass());
	
	 


	public AmigoInvisibleBO getBo() {
		return bo;
	}

	public void setBo(AmigoInvisibleBO bo) {
		this.bo = bo;
	}


	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String ticket=arg0.getParameter("ticket");
		try{
			Map out=new HashMap();
			Integer id=-1;
			if (ticket.startsWith("#")){
				ticket=ticket.substring(1);
				id = Integer.parseInt(ticket);
			}else{
				id=GroupIdEncrypter.decode(ticket);
			}
			List<Amigo> as=bo.recupera(id);
			out.put("id",id);
			out.put("amigos",as);
			return new ModelAndView("autodecodeok","out",out);
		}catch (EncodingException ee){
			return new ModelAndView("decodeerror","ticket",ticket);			
		}
	}

}
